/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.stereotype.Component;

/**
 *
 * @author mini-john
 */
@Component
public class AdamaMarshaller {

    @Autowired()
    @Qualifier(value = "castorMarshaller")
    Marshaller marshaller;
    @Autowired()
    @Qualifier(value = "castorMarshaller")
    Unmarshaller unmarshaller;

    public void marshal(Object dto, File file) throws FileNotFoundException, IOException {
        FileOutputStream fosXML = new FileOutputStream(file);
        marshaller.marshal(dto, new StreamResult(fosXML));
        fosXML.close();
    }

    public  <T> Object unmarshal( File file) throws FileNotFoundException, IOException {
        FileReader fosXML = new FileReader(file);
        Object object = unmarshaller.unmarshal(new StreamSource(fosXML));
        
        return object;
    }
}
